package com.yequan;

import com.yequan.work.*;

import java.math.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

public class DateDemo {

//    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date1 = sdf.parse("2020-11-12 15:56:56");
//        Date date2 = new Date();
//        System.out.println("date1: " + date2String3(date1));
//        System.out.println("date2: " + date2String3(date2));
//        System.out.println(date1.equals(date2));
//    }

    public static String date2String3(Date date) {
        return new SimpleDateFormat("yyyyMMdd").format(date);
    }


    public static void main(String[] args) throws Exception {
//        Date date = new Date(1610140851000L);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = simpleDateFormat.format(date);
//        System.out.println(format);
//        System.out.println(freshUser(1610140851000L));

//        System.out.println(getPlanStartTimeContent(new Date()));
//        System.out.println(getPlanStartTimeContent(DateUtil.addDateDay(new Date(), 1)));
//        System.out.println(getPlanStartTimeContent(DateUtil.string2Date("20210310120000")));
//        System.out.println(getPlanStartTimeContent(DateUtil.string2Date("20211110120000")));
//        System.out.println(getSingleRideTotalTime());


//        //乘客确认上车时间
//        Date passengerConfirmGetonTime = DateUtil.string2Date("20210310115509");
//        //乘客确认下车时间
//        Date passengerConfirmArriveTime = DateUtil.string2Date("20210310100000");
//
//        System.out.println(passengerConfirmArriveTime.compareTo(passengerConfirmGetonTime));

        addDate();

    }

    public static void addDate() throws Exception {
        Date date = DateUtil.string2Date("20211213000250");
        int timeInterval = 24;
        Date endDate = DateUtil.addDateField(date, timeInterval, Calendar.HOUR_OF_DAY);
        System.out.println(DateUtil.date2String8(endDate));
        if (System.currentTimeMillis() > endDate.getTime()) {
            System.out.println("out");
        }else {
            System.out.println("no");
        }
    }

    private static String getSingleRideTotalTime() throws ParseException {
        long hourMill = 3600000;
        //乘客确认上车时间
        Date passengerConfirmGetonTime = DateUtil.string2Date("20210310115509");
        //乘客确认下车时间
        Date passengerConfirmArriveTime = DateUtil.string2Date("20210310120000");
        //时间差，毫秒
        long diff = passengerConfirmArriveTime.getTime() - passengerConfirmGetonTime.getTime();
        String totalTime = null;
        if (diff < hourMill) {
            //60分钟以内
            float totalMinute = BigDecimal.valueOf(diff).divide(BigDecimal.valueOf(60000), 1, BigDecimal.ROUND_HALF_UP).floatValue();
            totalTime = totalMinute + "分钟";
        } else {
            //大于等于 60 分钟
            long hour = diff / hourMill;
            int partMinute = BigDecimal.valueOf(diff).subtract(BigDecimal.valueOf(hour * hourMill)).divide(BigDecimal.valueOf(60000), 0, BigDecimal.ROUND_HALF_UP).intValue();
            if (partMinute < 10) {
                totalTime = hour + "小时0" + partMinute + "分钟";
            } else {
                totalTime = hour + "小时" + partMinute + "分钟";
            }
        }
        return totalTime;
    }

    private static boolean freshUser(long certifyOpTime) throws ParseException {
        String firstCertifyTime = "2021-01-19 00:00:00";
        Calendar instance = Calendar.getInstance();
        instance.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(firstCertifyTime));
        return certifyOpTime > instance.getTimeInMillis();
    }

    /**
     * 根据乘客计划出发时间获取时间文案
     *
     * @param planStartTime
     * @return
     */
    private static String getPlanStartTimeContent(Date planStartTime) {
        String planStartTimeContent = "";
        try {
            Date current = new Date();
            if (DateUtil.isSameDate(planStartTime, current)) {
                //乘客计划出发时间就是今天
                planStartTimeContent = "今天" + DateUtil.date2String5(planStartTime);
            } else if (DateUtil.isSameDate(planStartTime, DateUtil.addDateDay(current, 1))) {
                //乘客计划出发时间是明天
                planStartTimeContent = "明天" + DateUtil.date2String5(planStartTime);
            } else {
                //乘客计划出发时间在明天之后
                planStartTimeContent = new SimpleDateFormat("M月dd日 HH:mm").format(planStartTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return planStartTimeContent;
    }

}
